/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.DAO;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import models.LoginStorage;
import views.TerminalView;
import views.AdminView;

/**
 *
 * @author Ali
 */
public class ControllerTerminal {

    private LoginStorage model;
    private TerminalView view;
    private DAO dao;
    private AdminView mainView;

    ControllerTerminal(LoginStorage model, TerminalView view) {
        this.model = model;
        this.view = view;
        this.dao = new DAO();
        this.view.addEnterButtonListener(new enter_btnListener());
        this.view.addRunAdminButtonListener(new runAdminListener());
        mainView = new AdminView();
    }

    public TerminalView getView() {
        return this.view;
    }

    public AdminView getMainView() {
        return this.mainView;
    }

    class enter_btnListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
//            System.out.println("Validate user");
            String[] input = view.getUserInput();
            if ((input[0].isEmpty()) || input[1].isEmpty()) {
                JOptionPane.showMessageDialog(null, "Please enter User ID and Password");
            } else if (validateUser()) {
                if (model.log(input[0])) {
                    if (model.getLoggedUsers() > 1) {
                        try {
                            JOptionPane.showMessageDialog(null, "Default Settings are set");
                            ResultSet set = dao.getSettingsOf("admin");
                            while (set.next()) {
                                for (int i = 4; i < set.getMetaData().getColumnCount(); i++) {
                                    System.out.print(set.getString(i) + " ");
                                }
                            }
                        } catch (SQLException ex) {
                            Logger.getLogger(ControllerTerminal.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } else {
                        try {
                            JOptionPane.showMessageDialog(null, "Personal Settings are set");
                            ResultSet set = dao.getSettingsOf(model.getLogList().get(0));
                            while (set.next()) {
                                for (int i = 4; i < set.getMetaData().getColumnCount(); i++) {
                                    System.out.print(set.getString(i) + " ");
                                }
                            }
                        } catch (SQLException ex) {
                            Logger.getLogger(ControllerTerminal.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } else if(model.getLoggedUsers()==1){
                    try {
                            JOptionPane.showMessageDialog(null, "Personal Settings are set");
                            ResultSet set = dao.getSettingsOf(model.getLogList().get(0));
                            while (set.next()) {
                                for (int i = 4; i < set.getMetaData().getColumnCount(); i++) {
                                    System.out.print(set.getString(i) + " ");
                                }
                            }
                        } catch (SQLException ex) {
                            Logger.getLogger(ControllerTerminal.class.getName()).log(Level.SEVERE, null, ex);
                        }
                }
            }
        }
    }

    class runAdminListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
//            System.out.println("Validate user");
            String[] input = view.getUserInput();
            if ((input[0].isEmpty()) || input[1].isEmpty()) {
                JOptionPane.showMessageDialog(null, "Please enter User ID and Password");
            } else if (validateAdmin()) {
                getMainView().setVisible(true);
                try {
                    getMainView().setVisible(true);
                    ControllerAdmin c = new ControllerAdmin(model, getMainView());
//                ControllerAdmin c = new ControllerAdmin(model, new AdminView());
                } catch (SQLException ex) {
                    Logger.getLogger(ControllerTerminal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public boolean validateUser() {
        String[] input = view.getUserInput();
        try {
            if (dao.getPasswordOf(input[0]).equals(input[1])) {
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(ControllerTerminal.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Error !!!");
        return false;
    }

    public boolean validateAdmin() {
        String[] input = view.getUserInput();
        try {
            if (dao.getPasswordOf(input[0]).equals(input[1])) {
                if (dao.isAdmin(input[0])) {
                    return true;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ControllerTerminal.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Error !!!");
        return false;
    }
}
